package com.ambercrm.tools;

import android.graphics.Bitmap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.yzq.zxinglibrary.encode.CodeCreator;

public class QRCodeUtils {
	public static Bitmap createQRCode(String url, int qr_width, int qr_height) {
		// 不带logo的话最后一个参数传null
		return CodeCreator.createQRCode(url, qr_width, qr_height, null);
	}


	public static Bitmap createQRCode(String url) {
		// 不带logo的话最后一个参数传null
		return CodeCreator.createQRCode(url, 400, 400, null);
	}

	public static Bitmap createQRCodeWithIcon(String url, Bitmap icon) {
		return CodeCreator.createQRCode(url, 400, 400, icon);
	}

	public static Bitmap createQRCodeWithIcon(String url, Bitmap icon, int width, int height) {
		return CodeCreator.createQRCode(url, width, height, icon);
	}

	public static Bitmap createBarCode(String content) {
		try {
			// 生成一维条码,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
			BitMatrix matrix = new MultiFormatWriter().encode(content,
					BarcodeFormat.CODE_128, 700, 200);
			int width = matrix.getWidth();
			int height = matrix.getHeight();
			int[] pixels = new int[width * height];
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					if (matrix.get(x, y)) {
						pixels[y * width + x] = 0xff000000;
					}
				}
			}

			Bitmap bitmap = Bitmap.createBitmap(width, height,
					Bitmap.Config.ARGB_8888);
			// 通过像素数组生成bitmap,具体参考api
			bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
			return bitmap;
		}
		catch (Exception e) {

		}

		return null;
	}
}
